package cypher;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Description: md5
 * Created by 4790K_NSL on 2017/6/10.
 */
public class UtilCypherMessageDigest {

    /** 根据参数，生成信息摘要
     * @param string
     * @param chartSetName
     * @param cypherName
     * @return
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     */
    public static  byte[]  digest(String string,String chartSetName, String cypherName) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        byte[] bytes=string.getBytes(chartSetName);
        MessageDigest md=MessageDigest.getInstance(cypherName);
        byte[] resultBytes=md.digest(bytes);
        return resultBytes;
    }



    /**利用MD5进行加密, 考虑效率问题的话，在读取文件的时候可以采用：DigestInputStream
     * @param str
     * @param chartSetName
     * @return
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException*/
    public  static  String getMd5InBase64(String str,String chartSetName) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        byte[] digestBytes=digest(str,chartSetName,"MD5");
        return UtilCypherBase64.encodeInBase64(digestBytes);
    }

    /**利用SHA-1进行加密, 生成信息摘要
     * @param str
     * @param chartSetName
     * @return
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException*/
    public  static  String getSha1InBase64(String str,String chartSetName) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        byte[] digestBytes=digest(str,chartSetName,"SHA-1");
        return UtilCypherBase64.encodeInBase64(digestBytes);
    }

    /**利用SHA-1进行加密, 生成信息摘要
     * @param str
     * @return
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException*/
    public  static  String getSha1InBase64(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        byte[] digestBytes=digest(str,"utf8","SHA-1");
        return UtilCypherBase64.encodeInBase64(digestBytes);
    }

    /**利用SHA-256进行加密, 生成信息摘要
     * @param str
     * @param chartSetName
     * @return
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException*/
    public  static  String getSha256InBase64(String str,String chartSetName) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        byte[] digestBytes=digest(str,chartSetName,"SHA-256");
        return UtilCypherBase64.encodeInBase64(digestBytes);
    }

    /**利用SHA-256进行加密, 生成信息摘要
     * @param str
     * @return
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException*/
    public  static  String getSha256InBase64(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        byte[] digestBytes=digest(str,"utf8","SHA-256");
        return UtilCypherBase64.encodeInBase64(digestBytes);
    }

    /**利用MD5进行加密
     * @param str
     * @return
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException*/
    public  static  String getMd5InBase64(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        return getMd5InBase64(str,"utf8");
    }




    public static void main(String[] args) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String passwd="nsl123321";
        String passwd1="你好";
        System.out.println(getMd5InBase64(passwd));
        System.out.println(getMd5InBase64(passwd1,"gbk"));
        System.out.println(getSha1InBase64(passwd));
        System.out.println(getSha256InBase64(passwd));
    }


}
